package com.castle.fortress.admin.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.goods.dto.GoodsDto;
import com.castle.fortress.admin.goods.entity.GoodsEntity;
import com.castle.fortress.admin.goods.mapper.GoodsMapper;
import com.castle.fortress.admin.goods.service.GoodsService;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 商品表 服务实现类
 *
 * @author majunjie
 * @since 2021-11-22
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {
    /**
    * 获取查询条件
    * @param goodsDto
    * @return
    */
    public QueryWrapper<GoodsEntity> getWrapper(GoodsDto goodsDto){
        QueryWrapper<GoodsEntity> wrapper= new QueryWrapper();
        if(goodsDto != null){
            GoodsEntity goodsEntity = ConvertUtil.transformObj(goodsDto,GoodsEntity.class);
            wrapper.like(StrUtil.isNotEmpty(goodsEntity.getName()),"name",goodsEntity.getName());
            wrapper.eq(goodsEntity.getCategoryId() != null,"category_id",goodsEntity.getCategoryId());
            wrapper.eq(goodsEntity.getType() != null,"type",goodsEntity.getType());
            wrapper.like(StrUtil.isNotEmpty(goodsEntity.getGoodsSn()),"goods_sn",goodsEntity.getGoodsSn());
            wrapper.eq(StrUtil.isNotEmpty(goodsEntity.getTotalCnf()),"total_cnf",goodsEntity.getTotalCnf());
            wrapper.eq(goodsEntity.getDispatchType() != null,"dispatch_type",goodsEntity.getDispatchType());
            wrapper.eq(goodsEntity.getIsNew() != null,"is_new",goodsEntity.getIsNew());
            wrapper.eq(goodsEntity.getIsHot() != null,"is_hot",goodsEntity.getIsHot());
            wrapper.eq(goodsEntity.getIsDiscount() != null,"is_discount",goodsEntity.getIsDiscount());
            wrapper.eq(goodsEntity.getIsRecommand() != null,"is_recommand",goodsEntity.getIsRecommand());
            wrapper.eq(goodsEntity.getIsSendFree() != null,"is_send_free",goodsEntity.getIsSendFree());
            wrapper.eq(goodsEntity.getIsAutoShelves() != null,"is_auto_shelves",goodsEntity.getIsAutoShelves());
            wrapper.eq(goodsEntity.getIsInvoice() != null,"is_invoice",goodsEntity.getIsInvoice());
            wrapper.eq(goodsEntity.getStatus() != null,"status",goodsEntity.getStatus());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<GoodsDto> pageGoods(Page<GoodsDto> page, GoodsDto goodsDto) {
        QueryWrapper<GoodsEntity> wrapper = getWrapper(goodsDto);
        Page<GoodsEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<GoodsEntity> goodsPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<GoodsDto> pageDto = new Page(goodsPage.getCurrent(), goodsPage.getSize(),goodsPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(goodsPage.getRecords(),GoodsDto.class));
        return pageDto;
    }

    @Override
    public IPage<GoodsDto> pageGoodsExtends(Page<GoodsDto> page, GoodsDto goodsDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        GoodsEntity goodsEntity = ConvertUtil.transformObj(goodsDto,GoodsEntity.class);
        List<GoodsEntity> goodsList=baseMapper.extendsList(pageMap,goodsEntity);
        Long total = baseMapper.extendsCount(goodsEntity);
        Page<GoodsDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(goodsList,GoodsDto.class));
        return pageDto;
    }
    @Override
    public GoodsDto getByIdExtends(Long id){
        GoodsEntity  goodsEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(goodsEntity,GoodsDto.class);
    }

    @Override
    public List<GoodsDto> listGoods(GoodsDto goodsDto){
        QueryWrapper<GoodsEntity> wrapper = getWrapper(goodsDto);
        List<GoodsEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,GoodsDto.class);
    }
}

